package com.sokal.easycare.modules.shares.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Maps;
import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.mapper.JsonMapper;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.modules.shares.entity.ExperienceSharing;
import com.sokal.easycare.modules.shares.entity.ExprShareFile;
import com.sokal.easycare.modules.shares.service.ExperienceSharingService;
import com.sokal.easycare.modules.shares.service.ExprShareFileService;

/**
 * 经验分享-结合软文分享、图像、视频Controller
 * @author lihaipei
 * @version 2017-03-30
 */
@Controller
@RequestMapping(value = "${adminPath}/shares/experienceSharing")
public class ExperienceSharingController extends BaseController {

	@Autowired
	private ExperienceSharingService experienceSharingService;
	
	@Autowired
	private ExprShareFileService exprShareFileService;
	
	@ModelAttribute
	public ExperienceSharing get(@RequestParam(required=false) String id) {
		ExperienceSharing entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = experienceSharingService.get(id);
		}
		if (entity == null){
			entity = new ExperienceSharing();
		}
		return entity;
	}
	
	@RequiresPermissions("shares:experienceSharing:view")
	@RequestMapping(value = {"list", ""})
	public String list(ExperienceSharing experienceSharing, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ExperienceSharing> page = experienceSharingService.findPage(new Page<ExperienceSharing>(request, response), experienceSharing); 
		model.addAttribute("page", page);
		return "modules/shares/experienceSharingList";
	}

	@RequiresPermissions("shares:experienceSharing:view")
	@RequestMapping(value = "form")
	public String form(ExperienceSharing experienceSharing, Model model) {
		model.addAttribute("experienceSharing", experienceSharing);
		return "modules/shares/experienceSharingForm";
	}

	@RequiresPermissions("shares:experienceSharing:edit")
	@RequestMapping(value = "save")
	public String save(ExperienceSharing experienceSharing, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, experienceSharing)){
			return form(experienceSharing, model);
		}
		experienceSharingService.save(experienceSharing);
		addMessage(redirectAttributes, "保存经验分享成功");
		return "redirect:"+Global.getAdminPath()+"/shares/experienceSharing/?repage";
	}
	
	@RequiresPermissions("shares:experienceSharing:edit")
	@RequestMapping(value = "delete")
	public String delete(ExperienceSharing experienceSharing, RedirectAttributes redirectAttributes) {
		experienceSharingService.delete(experienceSharing);
		addMessage(redirectAttributes, "删除经验分享成功");
		return "redirect:"+Global.getAdminPath()+"/shares/experienceSharing/?repage";
	}

	/**
	 * 跳转至文件管理页
	 * @author lihaipei
	 * @date 2017年3月30日 下午1:58:49
	 * @param exprShareId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toFileList")
	public String toFileList(String exprShareId, Model model) {
		ExperienceSharing exprSharing = new ExperienceSharing(exprShareId);
		ExprShareFile exprShareFile = new ExprShareFile();
		exprShareFile.setExprSharing(exprSharing);
		List<ExprShareFile> exprShareFiles = exprShareFileService.findList(exprShareFile);
		model.addAttribute("exprShareFiles", exprShareFiles);
		model.addAttribute("exprShareId", exprShareId);
		model.addAttribute("fastdfs_file_url", Global.getConfig("fastdfs_file_url"));
		return "modules/shares/fileList";
	}

	/**
	 * 保存经验分享图片
	 * @author lihaipei
	 * @date 2017年3月30日 下午2:02:02
	 * @param shareFile 
	 * @param exprShareId 经验分享id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "saveExprShareFile")
	public String saveExprShareFile(@RequestParam(value = "exprShareFile") MultipartFile exprShareFile, String exprShareId) {
		Map<String, Object> map = Maps.newHashMap();
		try {
			exprShareFileService.save(exprShareFile, exprShareId);
			map.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
		}
		return JsonMapper.nonDefaultMapper().toJson(map);
	}
	
	/**
	 * 删除经验分享图片
	 * @author lihaipei
	 * @date 2017年3月30日 下午2:03:16
	 * @param exprShareFileId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "deleteExprShareFile")
	public String deleteExprShareFile(String exprShareFileId) {
		Map<String, Object> map = Maps.newHashMap();
		ExprShareFile exprShareFile = new ExprShareFile(exprShareFileId);
		exprShareFileService.delete(exprShareFile);
		map.put("success", true);
		return JsonMapper.nonDefaultMapper().toJson(map);
	}
	
	/**
	 * 批量修改图片排序号
	 * @author lihaipei
	 * @date 2017年3月30日 下午2:03:36
	 * @param ids
	 * @param sorts
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "updateSort")
	public String updateSort(String[] ids, Integer[] sorts) {
		Map<String, Object> map = Maps.newHashMap();
    	for (int i = 0; i < ids.length; i++) {
    		ExprShareFile exprShareFile = new ExprShareFile(ids[i]);
    		exprShareFile.setSort(sorts[i]);
    		exprShareFileService.updateSort(exprShareFile);
    	}
		map.put("success", true);
		return JsonMapper.nonDefaultMapper().toJson(map);
	}
	
}